package com.heimo.agentlicence.task;


import java.util.List;

import org.apache.http.NameValuePair;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.heimo.agentlicence.http.HttpHelper;

public class BasicTask
		extends
		AsyncTask<Object/* url */, Integer /* Progress */, String /* Result */> {
	private final String TAG = "BasicTask";
	private Context ctx = null;
	
	//http://www.eaa.org.hk/Licensing/Licencelist/tabid/96/language/zh-HK/Default.aspx
	public static final String LOAD_LICENCE_LOAD_PAGE = "LOAD_LICENCE_LOAD_PAGE";
	public static final String LOAD_LICENCE_CHECK_PAGE = "LOAD_LICENCE_CHECK_PAGE";
	public static final String PATH_LICENCE_LOAD_PAGE = "/Licensing/Licencelist/tabid/96/language/zh-HK/Default.aspx";
	public static final String PATH_LICENCE_CHECK_PAGE = "/Licensing/Licencelist/tabid/96/language/zh-HK/Default.aspx";
	
	//http://www.eaa.org.hk/DisciplinaryProceedings/DisciplinaryRecordsSearch/tabid/659/language/zh-HK/Default.aspx
	public static final String LOAD_DISCIPLINE_LOAD_PAGE = "LOAD_DISCIPLINE_LOAD_PAGE";
	public static final String LOAD_DISCIPLINE_CHECK_PAGE = "LOAD_DISCIPLINE_CHECK_PAGE";
	public static final String PATH_DISCIPLINE_LOAD_PAGE = "/DisciplinaryProceedings/DisciplinaryRecordsSearch/tabid/659/language/zh-HK/Default.aspx";
	public static final String PATH_DISCIPLINE_CHECK_PAGE = "/DisciplinaryProceedings/DisciplinaryRecordsSearch/tabid/659/language/zh-HK/Default.aspx";

	
	private ProgressDialog pdialog;
	protected OnTaskCompleted listener;
	protected List<NameValuePair> nvps;
	
	protected String loadMsg = "Loading data from server...";
	
	public BasicTask(Context ctx) {
		this.ctx = ctx;
	}

	public BasicTask(Context ctx, OnTaskCompleted listener) {
		this(ctx);
		this.listener = listener;
	}
	
	public BasicTask(Context ctx, OnTaskCompleted listener,List<NameValuePair> nvps) {
		this(ctx);
		this.listener = listener;
		this.nvps = nvps;
	}

	/*
	 * Http Client Establish Connection
	 */
	@Override
	protected String doInBackground(Object... urls) {
		Object action = urls[0];
		String result = null;
		try {
			
			if (action == LOAD_LICENCE_LOAD_PAGE) {
				result = fnLoadLicence();
			}else if (action == LOAD_LICENCE_CHECK_PAGE) {
				nvps = (List<NameValuePair>)urls[1];
				result = fnCheckLicence();
			}else if (action == LOAD_DISCIPLINE_LOAD_PAGE) {
				result = fnLoadDiscipline();
			}else if(action == LOAD_DISCIPLINE_CHECK_PAGE){
				nvps = (List<NameValuePair>)urls[1];
				result = fnCheckDiscipline();
			}
		} catch (Exception e) {
			if (listener != null) {
				listener.onTaskFailed(e.getMessage());
			}
			Log.e(TAG, e.getMessage());
		}
		return result;
	}
	
	private String fnLoadLicence() throws Exception {
		Log.d(TAG, "fnLoadLicence()");
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doGet(PATH_LICENCE_LOAD_PAGE);			
	}
	
	private String fnCheckLicence() throws Exception {
		Log.d(TAG, "fnCheckLicence()");
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_LICENCE_CHECK_PAGE, nvps);			
	}
	
	private String fnLoadDiscipline() throws Exception {
		Log.d(TAG, "fnLoadDiscipline()");
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doGet(PATH_DISCIPLINE_LOAD_PAGE);			
	}
	
	private String fnCheckDiscipline() throws Exception {
		Log.d(TAG, "fnCheckDiscipline()");
		HttpHelper httpHelper = new HttpHelper();
		return httpHelper.doPost(PATH_DISCIPLINE_CHECK_PAGE, nvps);			
	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
	}

	@Override
	protected void onPreExecute() {
		if (pdialog == null) {
			pdialog = new ProgressDialog(ctx);
			pdialog.setCancelable(false);
			pdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			pdialog.setMessage(loadMsg);
		}
		pdialog.show();
	}

	@Override
	protected void onPostExecute(String result) {
		fnVoidCloseDialog();
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
	}

	protected void fnVoidCloseDialog() {
		if (pdialog != null) {
			pdialog.cancel();
		}
	}
}
